﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using TravisLight.Model.Entity;

namespace TravisLight.Model.Repository
{
    public class ApiRepository : IRepository
    {
        #region attributes

        public const string Endpoint = "https://api.travis-ci.org";

        #endregion

        public List<Repo> GetRepositoriesFor(string username)
        {
            List<Repo> repositories = new List<Repo>();

            using (WebClient client = new WebClient())
            {
                string json  = client.DownloadString(String.Format("{0}/repos?member={1}", Endpoint, username));
                repositories = JsonConvert.DeserializeObject<List<Repo>>(json);
            };

            return repositories.OrderBy(repository => repository.Rank).ToList();
        }
    }
}
